package com.zdl.interestingapp.utils

import com.zdl.interestingapp.utils.constants.UserDefinedString
import java.io.*

/**
 * @author zdl
 * @date 2018/4/23 16:41
 * email zdl328465042@163.com
 * description 字符串工具类
 */
object MyStringUtil {

    /**
     * 判断字符串是否为空
     * @param str 要判断的字符串
     * @return true:为空  false:不为空
     */
    fun isEmpty(str: String): Boolean {
        return "" == str.trim()
    }

    /**
     * 除去字符串中的空格、回车
     * @param str
     * @return String
     */
    fun handleEnterAndBlank(str: String): String {
        var result = str.trim()
        if (result.contains(UserDefinedString.ENTER)) {
            result = result.replace(UserDefinedString.ENTER, UserDefinedString.EMPTY_1)
        }
        return result
    }

    /**
     * list的深拷贝
     *
     * @param src 源list
     * @param <T> 泛型
     * @return list
     * @throws IOException IOException
     * @throws ClassNotFoundException ClassNotFoundException
     */
    @Throws(IOException::class, ClassNotFoundException::class)
    fun <T> deepCopy(src: List<T>): List<T> {
        val bos = ByteArrayOutputStream()
        val oos = ObjectOutputStream(bos)
        oos.writeObject(src)

        val bis = ByteArrayInputStream(bos.toByteArray())
        val ois = ObjectInputStream(bis)
        @Suppress("UNCHECKED_CAST")
        return ois.readObject() as List<T>
    }
}